//
//  Statu.swift
//  我的微博
//
//  Created by gongjunna on 15/8/1.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class Statu: NSObject {
    
//    /// 创建时间
//    var created_at: String?
//    /// 微博ID
//    var id: Int = 0
//    /// 微博信息内容
//    var text: String?
//    /// 微博来源
//    var source: String?
//    /// 配图数组
//    var pic_urls: [[String: String]]?
    var creatd_at : String?
    var id : Int = 0
    var text : String?
    var source : String?
    private var storedUrls : [NSURL]?
    var pic_urls : [[String: String]]? {
        didSet{
          storedUrls = [NSURL]()
            let key = "thumbnail_pic"
            if pic_urls?.count == 0 {
                
                return
            }
            for dict in pic_urls! {
                let value = dict[key]
                
                storedUrls?.append(NSURL(string: value!)!)
            }
        }
    }
    var  user: User?
    
    init(dict:[String :AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    var retweeted_status: Statu?
    
     var pictureUrls : [NSURL]? {
        return retweeted_status == nil ? storedUrls : retweeted_status?.storedUrls
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
                user = User(dict: dict)
            }
            return
        }
        if key == "retweeted_status" {
            if let dict = value as? [String: AnyObject] {
                retweeted_status = Statu(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    class   func getStatus(finishsed :(result:[Statu]?  ,error : NSError?) ->()) {
    
            NetWorkTools.shardNetWorkTool.loadState { (result, error) -> () in
                
                if error != nil {
                finishsed(result: nil, error: error!)
                    return
                }
                if  let array  : [[String :AnyObject]] = result!["statuses"] as! [[String :AnyObject]] {
                    
                    var list = [Statu]()
                    
                    for dict in array {
                        list.append(Statu(dict: dict))
                }
                    finishsed(result: list, error: nil)
                }
                else {
                
                    finishsed(result: nil, error: nil)
                }
                
                
                
    }
}
}